﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Model
{
  public class Init : MonoBehaviour
  {

    public string version;

    public static Init Instance;

    public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;


    public IStaticMethod start;

    public IStaticMethod update;

    void Awake()
    {
      Instance = this;
    }


    // Use this for initialization
    void Start()
    {
      StartCoroutine(LoadHotFixAssembly());
    }

    // Update is called once per frame
    void Update()
    {
      AppDomain.Invoke("Hotfix.Init", "Update", null, null);
    }


    IEnumerator LoadHotFixAssembly()
    {
      AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();

      //加载DLL文件
      ///?????????????????????
      WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/Hotfix.dll");
      while (!www.isDone)//如果资源未加载完
        yield return null;
      if (!string.IsNullOrEmpty(www.error))
        Debug.LogError(www.error);
      byte[] dll = www.bytes;
      www.Dispose();
      using (System.IO.MemoryStream fs = new System.IO.MemoryStream(dll))
      {
        AppDomain.LoadAssembly(fs, null, new Mono.Cecil.Pdb.PdbReaderProvider());
      }

      InitializeILRuntime();
      OnHotFixLoaded();
    }

    void InitializeILRuntime()
    {
      ///????????????
    }

    //调用
    void OnHotFixLoaded()
    {
      AppDomain.Invoke("Hotfix.Init", "Start", null, null);
    }

    

  }
}


